package com.gzlz.budejie.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.gzlz.budejie.R;
import com.gzlz.budejie.utils.Player;
import com.lidroid.xutils.BitmapUtils;
/**
 * 最新模块(声音)
 * @author LLX
 */
public class VoiceView extends RelativeLayout implements
		OnSeekBarChangeListener {

	private Button btn_voice;// 播放按钮
	private Player player;// 播放的工具类
	private SeekBar sb;// 进度控件
	private int progress;// 进度
	private RelativeLayout rl_seek;//进度控件布局
	private boolean isPlay = false;//判断当前是否播放  默认false
	private ImageView btn_state;//播放状态的按钮
	private String pathUrl = "http://abv.cn/music/光辉岁月.mp3";//模拟的Url
	private ImageView video_image;//展示的图片
	
	
	public VoiceView setAddress(String shengyinAddress,String tupianAddress){
		pathUrl = shengyinAddress;
		new BitmapUtils(getContext()).display(video_image, tupianAddress);
		return this;
	}
	
	
/**
 * 构造方法
 * @param context
 * @param attrs
 */
	public VoiceView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * 初始化
	 */
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		sb = (SeekBar) findViewById(R.id.sb);
		btn_voice = (Button) findViewById(R.id.bt_voice);
		rl_seek = (RelativeLayout) findViewById(R.id.rl_seek);
		btn_state = (ImageView) findViewById(R.id.btn_state);
		player = new Player(sb);
		video_image = (ImageView) findViewById(R.id.video_image);
		sb.setOnSeekBarChangeListener(this);
		// 设置播放按钮的点击事件
		btn_voice.setOnClickListener(new MyListener());
		//设置播放状态按钮的点击事件
		btn_state.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isPlay) {//true
					player.pause();
					btn_state.setBackgroundResource(R.drawable.ic_play_white);
					btn_voice.setVisibility(View.VISIBLE);
					isPlay = false;
				} else if (!isPlay) {//false
					player.play();
					btn_state.setBackgroundResource(R.drawable.ic_pause);
					btn_voice.setVisibility(View.GONE);
					isPlay = true;
				}

			}

		});

	}

	class MyStateListener implements OnClickListener {
		@Override
		public void onClick(View v) {
		}

	}

	class MyListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			rl_seek.setVisibility(View.VISIBLE);
			btn_voice.setVisibility(View.GONE);
			btn_state.setBackgroundResource(R.drawable.ic_pause);
			playForNet();

		}
	}

	/**
	 * 当进度改变的时候
	 */
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
		this.progress = progress * player.mediaPlayer.getDuration()
				/ seekBar.getMax();

	}

	public void playForNet() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				player.playUrl(pathUrl);
			}
		}) {
		}.start();

	}

	/**
	 * 通知用户已经开始一个触摸拖动手势。客户端可能需要使用这个来禁用seekbar的滑动功能。
	 */
	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	/**
	 * 通知用户触摸手势已经结束。户端可能需要使用这个来启用seekbar的滑动功能。
	 */
	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// seekTo()的参数是相对与影片时间的数字，而不是与seekBar.getMax()相对的数字
		player.mediaPlayer.seekTo(progress);

	}

}
